﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyWindowsService
{
    /// <summary>
    /// 文件系统监视服务客户端主窗体
    /// </summary>
    public partial class frmClient : Form
    {
        /// <summary>
        /// 服务已经安装标记
        /// </summary>
        private bool bolServiceInstalled = false;

        /// <summary>
        /// 初始化对象
        /// </summary>
        public frmClient()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmViewLog_Load(object sender, EventArgs e)
        {
            try
            {
                System.ServiceProcess.ServiceController control = new System.ServiceProcess.ServiceController("MyFileSystemWatcher");
                System.ServiceProcess.ServiceControllerStatus status = control.Status;
                control.Dispose();
                bolServiceInstalled = true;
            }
            catch (Exception ext)
            {
                lblServiceInstall.Text = "服务尚未安装";
                myTimer.Enabled = false;
                btnStartService.Enabled = false;
                btnStopService.Enabled = false;
                bolServiceInstalled = false;
                MessageBox.Show(this, "服务尚未安装:" + ext.Message, "系统错误");
            }
            this.btnRefresh_Click(null, null);
        }

        /// <summary>
        /// 刷新列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
            lvwRecord.BeginUpdate();
            try
            {
                lvwRecord.Items.Clear();
                using (System.Data.IDbCommand cmd = Util.DBConnection.CreateCommand())
                {
                    cmd.CommandText = "Select RecordID , ObjectName , WatchTime , EventStyle From FileSystemLog order by WatchTime";
                    System.Data.IDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        ListViewItem NewItem = new ListViewItem();
                        NewItem.Tag = Convert.ToString(reader.GetValue(0));
                        NewItem.Text = Convert.ToString(reader.GetValue(1));
                        NewItem.SubItems.Add(Convert.ToString(reader.GetValue(2)));
                        string Style = Convert.ToString(reader.GetValue(3));
                        NewItem.SubItems.Add(Style);
                        Style = Style.Trim().ToLower();
                        if (Style == "created")
                        {
                            NewItem.Group = lvwRecord.Groups[0];
                            NewItem.ImageIndex = 0;
                        }
                        else if (Style == "changed")
                        {
                            NewItem.Group = lvwRecord.Groups[1];
                            NewItem.ImageIndex = 1;
                        }
                        else if (Style == "deleted")
                        {
                            NewItem.Group = lvwRecord.Groups[2];
                            NewItem.ImageIndex = 2;
                        }
                        else if (Style == "renamed")
                        {
                            NewItem.Group = lvwRecord.Groups[3];
                            NewItem.ImageIndex = 3;
                        }
                        NewItem.StateImageIndex = NewItem.ImageIndex;
                        lvwRecord.Items.Add(NewItem);
                    }
                    reader.Close();
                }
                myStatus.Text = "共列出 " + lvwRecord.Items.Count + " 个记录";
            }
            catch (Exception ext)
            {
                MessageBox.Show(ext.ToString(), "系统错误");
            }
            this.Cursor = System.Windows.Forms.Cursors.Default;
            lvwRecord.EndUpdate();
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lvwRecord.SelectedItems.Count > 0)
            {
                using (System.Data.IDbCommand cmd = Util.DBConnection.CreateCommand())
                {
                    for (int iCount = lvwRecord.Items.Count - 1; iCount >= 0; iCount--)
                    {
                        ListViewItem item = lvwRecord.Items[iCount];
                        if (item.Selected)
                        {
                            cmd.CommandText = "Delete From FileSystemLog Where RecordID = '" + item.Tag + "'";
                            cmd.ExecuteNonQuery();
                            lvwRecord.Items.Remove(item);
                        }
                    }//for
                }//using
            }
        }
        /// <summary>
        /// 系统配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfig_Click(object sender, EventArgs e)
        {
            using (dlgConfig dlg = new dlgConfig())
            {
                dlg.ShowDialog(this);
            }
        }

        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStartService_Click(object sender, EventArgs e)
        {
            if (bolServiceInstalled == false)
                return;
            using (System.ServiceProcess.ServiceController control = new System.ServiceProcess.ServiceController("MyFileSystemWatcher"))
            {
                if (control.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                {
                    control.Start();
                }
            }
        }
        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStopService_Click(object sender, EventArgs e)
        {
            if (bolServiceInstalled == false)
                return;
            using (System.ServiceProcess.ServiceController control = new System.ServiceProcess.ServiceController("MyFileSystemWatcher"))
            {
                if (control.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                {
                    control.Stop();
                }
            }
        }

        /// <summary>
        /// 每隔2秒刷新按钮状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void myTimer_Tick(object sender, EventArgs e)
        {
            if (bolServiceInstalled == false)
                return;
            using (System.ServiceProcess.ServiceController control = new System.ServiceProcess.ServiceController("MyFileSystemWatcher"))
            {
                btnStartService.Enabled = (control.Status == System.ServiceProcess.ServiceControllerStatus.Stopped);
                btnStopService.Enabled = (control.Status == System.ServiceProcess.ServiceControllerStatus.Running);
            }
        }

        private void myLinkLabel_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.xdesigner.cn");
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show(
                this,
                @"    本程序是袁永福于2008年10月为《C#发现之旅》系列课程编写的实现Windows Service的演示程序，《C#发现之旅》是袁永福编写的针对C#中高级开发者的编程技术的系列教程，详情请查看网站 http://www.xdesigner.cn ，袁永福电子邮箱 yyf9989@hotmail.com，请尊重知识产权。",
                "关于本程序");
        }
    }
}